<?php
namespace Admin\Controller;

use Think\Controller;

class ComplaintController extends RightController
{
    // 加载初始化内容页,并检索
    public function index()
    {
        //判断分页时搜索条件
        if (I("get.p")) {
            $where = session("QG_SEARCH_WHERE_complaint");
			$params = session("QG_SEARCH_PARAMS_complaint");
            $where = empty($where) ? "1" : $where;
            $pageSize = session("QG_SEARCH_SIZE_complaint");
        } else {
            //清除session存储
            session("QG_SEARCH_WHERE_complaint", null);
            session("QG_SEARCH_PARAMS_complaint", null);
            session("QG_SEARCH_SIZE_complaint", null);
            $pageSize = I("post.pageSize");
			$params = I("post.");

            $where = "1";
        }

        // POST数据提交，重新组合检索条件
        if (IS_POST) {
            unset($_GET["p"]);
            $where = $this->searchCondition();
        }
		
		// 分页输入
        if(empty($pageSize)){
            $pageSize = 10;
        }

        // 总条数
        $count = M('complaint')
            ->alias("t")
            ->where($where)
            ->count();
        $page = new \Admin\Model\Page($count, $pageSize);
        $show = $page->show();

        // 进行分页数据查询
        $list = M('complaint')
            ->alias("t")
            ->join("left join member m on t.account=m.account")
            ->field("t.*,m.nickname name")
            ->where($where)
            ->order('t.id DESC')
            ->limit($page->firstRow . ',' . $page->listRows)->select();
			
		// 统计
        $sum = M('complaint')
            ->alias("t")
            ->field("count(1) as countNum")
            ->where($where)
            ->order('t.id DESC')
            ->find();

        // 渲染查询条件到session
        session("QG_SEARCH_WHERE_complaint", $where);
		session("QG_SEARCH_PARAMS_complaint", $params);
		session("QG_SEARCH_SIZE_complaint", $pageSize);
		
		// 输出数据
        $this->assign("params", $params);
        $this->assign('list', $list);
        $this->assign('sum', $sum);
		$this->assign('lists', array(
            "page" => getDropdownList("pageSize" ,"" ,false)
        ));
        $this->assign('page', $show);
        $this->display();
    }

    // 搜索条件
    public function searchCondition()
    {
        $where = "1";
        //
        if (I('post.account')) {
            $where .= " AND t.account='" . I("post.account") . "'";
        }
        return $where;
    }

    // 删除
    public function delete()
    {
        if (IS_POST) {
            // ID
            $id = intval(I("post.id"));
			
			// 没有ID
			if(empty($id)){
				$this->ajaxError("错误！");
			}
            //删除数据
            $result = M('complaint')->where(array("id" => $id))->delete();

            // 如果删除成功
            if ($result) {
				$this->ajaxSuccess("删除成功！");
            } else {
				$this->ajaxError("删除失败！");
            }
        }
    }
}